home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk1 / color_twiddler / colorreq.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  17KB  |  722 lines

  1.  
  2. /* I had nothing to do with this section other than removing a couple of */
  3. /* Latticisms. -- John */
  4.  
  5. /* This color requester was hacked out of a Mandelbrot program by
  6.    RJ Mical, Mike Ballantyne and R French.  I'm not sure which one of these
  7. deserves most of the credit for the colour requester but I suspect RJ Mical.
  8. I merely neatened up the code, fixed it so it wouldn't go glitchy with
  9. fast ram, got rid of 33 unnecessary gadgets, put in the numeric display of
  10. the rgb values of the current colour, fixed the display so it looks
  11. reasonable in less than 5 bit planes (it used to always try to show 32
  12. coloured squares, even if it only had two colours to work with), and made it
  13. as easy as possible to call from any program.  To access it from your
  14. program simply put in the statement  DoColorWindow(screen)  where screen is
  15. a pointer to your screen.  That's it.  This program (subroutine actually)
  16. may be freely distributed as long as this notice giving credit to the four
  17. contributing authors is left intact.  Pls address any correspondence
  18. regarding this program to :
  19.       Bruce Dawson
  20.       c/o CantorSoft.
  21.       407-1280 Haro St,
  22.       Vancouver
  23.       B.C.
  24.       V6E1E8
  25.  
  26.                 */
  27.  
  28.  
  29.  
  30.  
  31.  
  32. #include <clib/macros.h>
  33. #include <exec/types.h>
  34. #include <exec/tasks.h>
  35. #include <exec/devices.h>
  36. #include <devices/keymap.h>
  37. #include <graphics/copper.h>
  38. #include <graphics/display.h>
  39. #include <graphics/gels.h>
  40. #include <graphics/regions.h>
  41. #include <hardware/blit.h>
  42. #include <intuition/intuition.h>
  43. #include <intuition/intuitionbase.h>
  44.  
  45. /* Lattice sticks these two in a subdirectory. -- John */
  46.  
  47. #include <ctype.h>
  48. #include <stdio.h>
  49.  
  50. #include <libraries/dos.h>
  51.  
  52. /* === the definitions for the ColorWindow =============================== */
  53. #define KNOB_BODY      0x1111
  54.  
  55. #define WINDOW_WIDTH   208
  56. #define WINDOW_HEIGHT  106
  57.  
  58. #define WINDOW_LEFT   (320 - WINDOW_WIDTH)
  59. #define WINDOW_TOP   (200 - WINDOW_HEIGHT)
  60.  
  61. #define CHARACTER_WIDTH      8
  62. #define CHARACTER_HEIGHT   8
  63.  
  64. #define CURRENT_LEFT      6
  65. #define CURRENT_TOP     16
  66. #define CURRENT_RIGHT       (CURRENT_LEFT + 15)
  67. #define CURRENT_BOTTOM     (CURRENT_TOP + 29)
  68.  
  69. #define COLORS_TOP    55
  70.  
  71. #define PROP_LEFT      38
  72. #define PROP_TOP       14
  73. #define PROP_WIDTH    149
  74. #define PROP_HEIGHT    10
  75.  
  76. #define ACTIONS_LEFT  141
  77. #define ACTIONS_TOP    51
  78. #define ACTIONS_WIDTH    (CHARACTER_WIDTH * 6 + 4)
  79. #define ACTIONS_HEIGHT     9
  80.  
  81. /* GREEN and RED are out of order.  Do you wonder why?    Some day I'll
  82.  * tell you.  =RJ=
  83.  */
  84. #define COLOR_COPY          0
  85. #define COLOR_RANGE          1
  86. #define COLOR_OK          2
  87. #define COLOR_CANCEL          3
  88. #define COLOR_GREEN          4
  89. #define COLOR_RED          5
  90. #define COLOR_BLUE          6
  91. #define COLOR_GADGETS_COUNT   7
  92.  
  93.  
  94.  
  95. /***************************************************************************
  96.  *
  97.  * Color window template initialization and routines,
  98.  * for colorwindow and cyclewindow
  99.  *
  100.  * Throughout this file, the COLOR_RED and COLOR_GREEN gadgets are in the
  101.  * opposite order you would expect.
  102.  *
  103.  ****************************************************************************/
  104.  
  105. struct TextAttr safefont = {
  106.    (UBYTE *)"topaz.font",
  107.    TOPAZ_EIGHTY,
  108.    0,
  109.    0,
  110. };
  111.  
  112. struct IntuiText string =
  113. {
  114.    1, 0,         /* frontpen, backpen */
  115.    JAM2,         /* drawmode      */
  116.    0,0,          /* leftedge, topedge */
  117.    &safefont,         /* pointer to font   */
  118.    NULL,         /* pointer to text data */
  119.    NULL          /* next text */
  120. };
  121.  
  122.  
  123. SHORT actions_corners[] = {
  124.    -1, -1,
  125.    -1, ACTIONS_HEIGHT,
  126.    ACTIONS_WIDTH, ACTIONS_HEIGHT,
  127.    ACTIONS_WIDTH, -1,
  128.    -1, -1,
  129. };
  130.  
  131. struct Border actions_border = {
  132.    0, 0,
  133.    1, 0,
  134.    JAM1,
  135.    5,
  136.    actions_corners,
  137.    NULL,
  138. };
  139.  
  140. struct IntuiText actions_text[4] = {
  141.    { /* "COPY" */
  142.       1, 0,
  143.       JAM2,
  144.       2 + CHARACTER_WIDTH, 1,
  145.       &safefont,
  146.       (UBYTE *)"COPY",
  147.       NULL,
  148.    },
  149.    { /* "RANGE" */
  150.       1, 0,
  151.       JAM2,
  152.       2 + (CHARACTER_WIDTH >> 1), 1,
  153.       &safefont,
  154.       (UBYTE *)"RANGE",
  155.       NULL,
  156.    },
  157.    { /* "OK" */
  158.       1, 0,
  159.       JAM2,
  160.       2 + (CHARACTER_WIDTH << 1), 1,
  161.       &safefont,
  162.       (UBYTE *)"OK",
  163.       NULL,
  164.    },
  165.    { /* "CANCEL" */
  166.       1, 0,
  167.       JAM2,
  168.       2, 1,
  169.       &safefont,
  170.       (UBYTE *)"CANCEL",
  171.       NULL,
  172.    },
  173. };
  174.  
  175.  
  176. /* ======================================================================== */
  177. /* ======================================================================== */
  178. /* ======================================================================== */
  179. /* RJM anchor */
  180. struct Image   ColorPropsImages[3];
  181. struct Image   ColorImage =
  182.    {
  183.    CURRENT_LEFT, COLORS_TOP,           /* left edge, top edge */
  184.    15, 10, 0,                   /* width, height, depth */
  185.    NULL,                   /* image data */
  186.    0,                       /* planepick */
  187.    0,                       /* planeonoff - set later */
  188.    NULL                    /* NextImage */
  189.    };
  190.  
  191. struct PropInfo ColorPropsInfos[3] = {
  192.    { /* COLOR_GREEN */
  193.       AUTOKNOB | FREEHORIZ,
  194.       0,
  195.       0,
  196.       KNOB_BODY,
  197.       0,
  198.       0, 0, 0, 0, 0, 0,
  199.    },
  200.    { /* COLOR_RED */
  201.       AUTOKNOB | FREEHORIZ,
  202.       0,
  203.       0,
  204.       KNOB_BODY,
  205.       0,
  206.       0, 0, 0, 0, 0, 0,
  207.    },
  208.    { /* COLOR_BLUE */
  209.       AUTOKNOB | FREEHORIZ,
  210.       0,
  211.       0,
  212.       KNOB_BODY,
  213.       0,
  214.       0, 0, 0, 0, 0, 0,
  215.    },
  216. };
  217.  
  218. struct Gadget gadglist[COLOR_GADGETS_COUNT] = {
  219.    { /* COLOR_COPY */
  220.       NULL,
  221.       ACTIONS_LEFT,
  222.       ACTIONS_TOP + (0 * (ACTIONS_HEIGHT + 3)),
  223.       ACTIONS_WIDTH,
  224.       ACTIONS_HEIGHT,
  225.       GADGHCOMP,
  226.       RELVERIFY,
  227.       BOOLGADGET,
  228.       (APTR)&actions_border,
  229.       NULL,
  230.       &actions_text[0],
  231.       NULL,
  232.       NULL,
  233.       COLOR_COPY,
  234.       NULL,
  235.    },
  236.    { /* COLOR_RANGE */
  237.       &gadglist[COLOR_COPY],
  238.       ACTIONS_LEFT,
  239.       ACTIONS_TOP + (01 * (ACTIONS_HEIGHT + 3)),
  240.       ACTIONS_WIDTH,
  241.       ACTIONS_HEIGHT,
  242.       GADGHCOMP,
  243.       RELVERIFY,
  244.       BOOLGADGET,
  245.       (APTR)&actions_border,
  246.       NULL,
  247.       &actions_text[01],
  248.       NULL,
  249.       NULL,
  250.       COLOR_RANGE,
  251.       NULL,
  252.    },
  253.    { /* COLOR_OK */
  254.       &gadglist[COLOR_RANGE],
  255.       ACTIONS_LEFT,
  256.       ACTIONS_TOP + (02 * (ACTIONS_HEIGHT + 3)),
  257.       ACTIONS_WIDTH,
  258.       ACTIONS_HEIGHT,
  259.       GADGHCOMP,
  260.       RELVERIFY,
  261.       BOOLGADGET,
  262.       (APTR)&actions_border,
  263.       NULL,
  264.       &actions_text[02],
  265.       NULL,
  266.       NULL,
  267.       COLOR_OK,
  268.       NULL,
  269.    },
  270.    { /* COLOR_CANCEL */
  271.       &gadglist[COLOR_OK],
  272.       ACTIONS_LEFT,
  273.       ACTIONS_TOP + (03 * (ACTIONS_HEIGHT + 3)),
  274.       ACTIONS_WIDTH,
  275.       ACTIONS_HEIGHT,
  276.       GADGHCOMP,
  277.       RELVERIFY,
  278.       BOOLGADGET,
  279.       (APTR)&actions_border,
  280.       NULL,
  281.       &actions_text[03],
  282.       NULL,
  283.       NULL,
  284.       COLOR_CANCEL,
  285.       NULL,
  286.    },
  287.    { /* COLOR_GREEN */
  288.       &gadglist[COLOR_CANCEL],
  289.       PROP_LEFT,
  290.       PROP_TOP + (01 * (PROP_HEIGHT + 1)),
  291.       PROP_WIDTH,
  292.       PROP_HEIGHT,
  293.       GADGHCOMP | GADGIMAGE,
  294.       FOLLOWMOUSE,
  295.       PROPGADGET,
  296.       (APTR)&ColorPropsImages[01],
  297.       NULL,
  298.       NULL,
  299.       NULL,
  300.       (APTR)&ColorPropsInfos[01],
  301.       COLOR_GREEN,
  302.       NULL,
  303.    },
  304.    { /* COLOR_RED */
  305.       &gadglist[COLOR_GREEN],
  306.       PROP_LEFT,
  307.       PROP_TOP + (00 * (PROP_HEIGHT + 1)),
  308.       PROP_WIDTH,
  309.       PROP_HEIGHT,
  310.       GADGHCOMP | GADGIMAGE,
  311.       FOLLOWMOUSE,
  312.       PROPGADGET,
  313.       (APTR)&ColorPropsImages[00],
  314.       NULL,
  315.       NULL,
  316.       NULL,
  317.       (APTR)&ColorPropsInfos[00],
  318.       COLOR_RED,
  319.       NULL,
  320.    },
  321.    { /* COLOR_BLUE */
  322.       &gadglist[COLOR_RED],
  323.       PROP_LEFT,
  324.       PROP_TOP + (02 * (PROP_HEIGHT + 1)),
  325.       PROP_WIDTH,
  326.       PROP_HEIGHT,
  327.       GADGHCOMP | GADGIMAGE,
  328.       FOLLOWMOUSE,
  329.       PROPGADGET,
  330.       (APTR)&ColorPropsImages[02],
  331.       NULL,
  332.       NULL,
  333.       NULL,
  334.       (APTR)&ColorPropsInfos[02],
  335.       COLOR_BLUE,
  336.       NULL,
  337.    },
  338. };
  339.  
  340.  
  341. /*----------------------*/
  342. /* Graphics definitions */
  343.  
  344. struct     RastPort    *rstport;
  345. struct     ViewPort    *vp;
  346.  
  347. struct     Window      *ColorWindow;
  348.  
  349. /*****************************************************************************
  350.  *
  351.  * Color initialization and routines
  352.  *
  353.  ****************************************************************************/
  354.  
  355. #define COPYCOLOR    1
  356. #define RANGE_FIRST    2
  357. #define RANGE_SECOND    3
  358.  
  359. USHORT     ColorMode;
  360.  
  361. USHORT     SavePalette[32];
  362.  
  363. #define COLOR_IDCMP_FLAGS (GADGETDOWN | GADGETUP | MOUSEBUTTONS \
  364.       | MENUPICK | MOUSEMOVE | ACTIVEWINDOW | INACTIVEWINDOW)
  365.  
  366. struct NewWindow ColorNewWindow =
  367.    {
  368.    WINDOW_LEFT, WINDOW_TOP,
  369.    WINDOW_WIDTH, WINDOW_HEIGHT,
  370.    -1, -1,
  371.    COLOR_IDCMP_FLAGS,
  372.    WINDOWDRAG | SMART_REFRESH | NOCAREREFRESH | ACTIVATE,
  373.    NULL,             /* struct Gadget *FirstGadget; */
  374.    NULL,             /* struct Image *CheckMark; */
  375.    "Modify Colours.",  /* the title text for this window */
  376.    NULL,             /* struct Screen *Screen; */
  377.    NULL,             /* struct BitMap *BitMap; */
  378.    0, 0,
  379.    0, 0,
  380.    CUSTOMSCREEN,         /* USHORT Type; */
  381.    };
  382.  
  383. USHORT     RangeFirst;   /* the first selection of the range-color pair */
  384.  
  385. /* ======================================================================== */
  386. /* ======================================================================== */
  387.  
  388. BOOL OpenColorWindow(screen, colours)
  389. struct Screen *screen;
  390. short           colours;
  391. {
  392.    REGISTER LONG   i;
  393.  
  394.    ColorNewWindow.Screen = screen;
  395.    ColorNewWindow.FirstGadget=&gadglist[COLOR_GADGETS_COUNT - 1];
  396.    ColorMode = NULL;
  397.    if ( ! (ColorWindow = (struct Window *)OpenWindow(&ColorNewWindow)))
  398.       return(FALSE);
  399.    rstport = ColorWindow->RPort;
  400.    SetAPen(rstport, 1);
  401.    SetColorProps();
  402.    for (i = 0; i < 32; i++)
  403.       SavePalette[i] = GetRGB4(vp->ColorMap, i);
  404.    DrawColorWindow(colours);
  405.    return(TRUE);
  406. }
  407.  
  408. VOID CloseColorWindow(accept)
  409. BOOL accept;
  410. {
  411.    if ( NOT ColorWindow)
  412.       return;
  413.    CloseWindow(ColorWindow);
  414.    ColorWindow = NULL;
  415.    if (NOT accept)
  416.       LoadRGB4(vp, &SavePalette[0], 32L);
  417. }
  418.  
  419. VOID ColorRange(first, last)
  420. SHORT first, last;
  421. {
  422.    REGISTER SHORT   i;
  423.    LONG      whole, redfraction, greenfraction, bluefraction;
  424.    REGISTER USHORT   rgb;
  425.    SHORT      firstred, firstgreen, firstblue;
  426.    SHORT      lastred, lastgreen, lastblue;
  427.    SHORT      workred, workgreen, workblue;
  428.  
  429.    if (first > last) {
  430.       i = first;
  431.       first = last;
  432.       last = i;
  433.    }
  434.    /* I need to see a spread of at least two, where there's at least one
  435.     * spot between the endpoints, else there's no work to do so I
  436.     * might as well just return now.
  437.     */
  438.    if (first >= last - 1)
  439.       return;
  440.  
  441.    rgb = GetRGB4(vp->ColorMap, (LONG)first);
  442.    firstred = (rgb >> 8) & 0xF;
  443.    firstgreen = (rgb >> 4) & 0xF;
  444.    firstblue = (rgb >> 0) & 0xF;
  445.  
  446.    rgb = GetRGB4(vp->ColorMap, (LONG)last);
  447.    lastred = (rgb >> 8) & 0xF;
  448.    lastgreen = (rgb >> 4) & 0xF;
  449.    lastblue = (rgb >> 0) & 0xF;
  450.  
  451.    whole = ((LONG) (lastred - firstred)) << 16;
  452.    redfraction = whole / (last - first);
  453.    whole = ((LONG)(lastgreen - firstgreen)) << 16;
  454.    greenfraction = whole / (last - first);
  455.    whole = ((LONG)(lastblue - firstblue)) << 16;
  456.    bluefraction = whole / (last - first);
  457.  
  458.    for (i = first + 1; i < last; i++)
  459.       {
  460.       lastred = (redfraction * (i - first) + 0x8000) >> 16;
  461.       workred = firstred + lastred;
  462.       lastgreen = (greenfraction * (i - first) + 0x8000) >> 16;
  463.       workgreen = firstgreen + lastgreen;
  464.       lastblue = (bluefraction * (i - first) + 0x8000) >> 16;
  465.       workblue = firstblue + lastblue;
  466.       SetRGB4(vp, (LONG) i,(LONG)workred,(LONG)workgreen,(LONG)workblue);
  467.       }
  468. }
  469.  
  470. BOOL colorchosen(pen)
  471. short pen;
  472. {
  473.    REGISTER USHORT   rgb;
  474.  
  475.    complementbox(rstport, rstport->FgPen);
  476.    complementbox(rstport, pen);
  477.  
  478.    if (ColorMode == COPYCOLOR)
  479.       {
  480.       rgb = GetRGB4(vp->ColorMap, (LONG)rstport->FgPen);
  481.       SetRGB4(vp,  pen, (rgb >> 8), (rgb >> 4), rgb );
  482.       ColorMode = NULL;
  483.       }
  484.    if (ColorMode == RANGE_FIRST)
  485.       {
  486.       ColorMode = RANGE_SECOND;
  487.       RangeFirst = pen;
  488.       }
  489.    else if (ColorMode == RANGE_SECOND)
  490.       {
  491.       ColorMode = NULL;
  492.       ColorRange(RangeFirst, pen);
  493.       }
  494.  
  495.    SetAPen(rstport, (LONG)pen);
  496.    rgb = GetRGB4(vp->ColorMap, (LONG)pen);
  497.    ColorRectFill((LONG)pen);
  498.  
  499.    SetColorProps();
  500. }
  501.  
  502.  
  503.  
  504. BOOL GadgetGotten(gadgetnum)
  505. short gadgetnum;
  506. {
  507.    switch (gadgetnum)
  508.       {
  509.       case COLOR_OK:
  510.      CloseColorWindow(TRUE);
  511.      return(FALSE);
  512.       case COLOR_CANCEL:
  513.      CloseColorWindow(FALSE);
  514.      return(FALSE);
  515.       case COLOR_COPY:
  516.      ColorMode = COPYCOLOR;
  517.      break;
  518.       case COLOR_RANGE:
  519.      ColorMode = RANGE_FIRST;
  520.      break;
  521.       }
  522.    return(TRUE);
  523. }
  524.  
  525.  
  526. ModifyColors()
  527. {
  528.    USHORT      pen;
  529.    REGISTER USHORT   newred, newgreen, newblue;
  530.  
  531.    pen = rstport->FgPen;
  532.    newred = ((struct PropInfo *)
  533.           gadglist[COLOR_RED].SpecialInfo)->HorizPot >> 12;
  534.    newgreen = ((struct PropInfo *)
  535.           gadglist[COLOR_GREEN].SpecialInfo)->HorizPot >> 12;
  536.    newblue = ((struct PropInfo *)
  537.           gadglist[COLOR_BLUE].SpecialInfo)->HorizPot >> 12;
  538.    SetRGB4(vp, (LONG)pen, (LONG)newred, (LONG)newgreen, (LONG)newblue);
  539.  
  540.    printcolors(newred, newgreen, newblue);
  541. }
  542.  
  543.  
  544.  
  545. printcolors(red, green, blue)
  546. USHORT     red, green, blue;
  547. {
  548.    char *buffer = "  ";
  549.  
  550.    sprintf(buffer, "%2d", red);
  551.    printtext(buffer, PROP_WIDTH + PROP_LEFT+2, CURRENT_TOP);
  552.  
  553.    sprintf(buffer, "%2d", green);
  554.    printtext(buffer, PROP_WIDTH + PROP_LEFT+2, CURRENT_TOP+(PROP_HEIGHT+1));
  555.  
  556.    sprintf(buffer, "%2d", blue);
  557.    printtext(buffer, PROP_WIDTH + PROP_LEFT+2, CURRENT_TOP
  558.                          + (PROP_HEIGHT+1) * 2);
  559. }
  560.  
  561.  
  562.  
  563. DrawColorWindow(colours)
  564. {
  565.    REGISTER SHORT i;
  566.  
  567.    ColorRectFill((LONG)rstport->FgPen);
  568.    DrawBox(rstport, CURRENT_LEFT-2,CURRENT_TOP-2,CURRENT_RIGHT+2,CURRENT_BOTTOM+2);
  569.    DrawBox(rstport, CURRENT_LEFT-2, COLORS_TOP-2, CURRENT_LEFT+(8 * 15)+1,
  570.                     COLORS_TOP + (4 * 10) + 1);
  571.    printtext("R", CURRENT_RIGHT + 6, CURRENT_TOP);
  572.    printtext("G", CURRENT_RIGHT + 6, CURRENT_TOP + PROP_HEIGHT+1);
  573.    printtext("B", CURRENT_RIGHT + 6, CURRENT_TOP + (PROP_HEIGHT+1)* 2);
  574.  
  575.    for (i = 0; i < colours; i++)
  576.       {
  577.       ColorImage.PlaneOnOff = i;
  578.       DrawImage(rstport, &ColorImage, (i % 8) * 15, (i / 8) * 10);
  579.       }
  580.    complementbox(rstport, 1);        /* starting colour is colour 1 */
  581. }
  582.  
  583.  
  584.  
  585. SetColorProps()
  586. {
  587.    USHORT      rgb;
  588.    REGISTER USHORT   red, green, blue;
  589.    SHORT      greenpos, redpos, bluepos;
  590.  
  591.    redpos = RemoveGadget(ColorWindow, &gadglist[COLOR_RED]);
  592.    greenpos = RemoveGadget(ColorWindow, &gadglist[COLOR_GREEN]);
  593.    bluepos = RemoveGadget(ColorWindow, &gadglist[COLOR_BLUE]);
  594.  
  595.    rgb = GetRGB4(vp->ColorMap, (LONG)rstport->FgPen);
  596.    red = (rgb >> 8) & 0xF;
  597.    green = (rgb >> 4) & 0xF;
  598.    blue = (rgb >> 0) & 0xF;
  599.  
  600.    ((struct PropInfo *)gadglist[COLOR_RED].SpecialInfo)->HorizPot
  601.            = (red << 12) | (red << 8) | (red << 4) | red;
  602.    ((struct PropInfo *)gadglist[COLOR_GREEN].SpecialInfo)->HorizPot
  603.            = (green << 12) | (green << 8) | (green << 4) | green;
  604.    ((struct PropInfo *)gadglist[COLOR_BLUE].SpecialInfo)->HorizPot
  605.            = (blue << 12) | (blue << 8) | (blue << 4) | blue;
  606.  
  607.    AddGadget(ColorWindow,&gadglist[COLOR_BLUE], (LONG)bluepos);
  608.    AddGadget(ColorWindow,&gadglist[COLOR_GREEN],(LONG)greenpos);
  609.    AddGadget(ColorWindow,&gadglist[COLOR_RED], (LONG)redpos);
  610.  
  611.    RefreshGadgets(&gadglist[COLOR_GADGETS_COUNT-1],ColorWindow);
  612.  
  613.    printcolors(red, green, blue);
  614. }
  615.  
  616.  
  617. ColorRectFill( pen)
  618. LONG pen;
  619. {
  620.    SetAPen(rstport, pen);
  621.    SetDrMd(rstport, JAM2);
  622.    WaitBOVP(vp);
  623.    RectFill(rstport, CURRENT_LEFT, CURRENT_TOP, CURRENT_RIGHT, CURRENT_BOTTOM);
  624. }
  625.  
  626. VOID DoColorWindow(screen)
  627. struct Screen *screen;
  628. {
  629.    BOOL        GadgetGotten(), OpenColorWindow();
  630.    REGISTER struct IntuiMessage   *message;
  631.    REGISTER ULONG          class;
  632.    REGISTER struct Gadget      *gadget;
  633.    USHORT   bitplanes, colours, code;
  634.    short    number, x, y;
  635.  
  636.  
  637.    vp = &screen->ViewPort;
  638.    bitplanes = screen->BitMap.Depth;
  639.    for (colours = 1; bitplanes; bitplanes--)
  640.       colours *= 2;
  641.    if (colours > 32)
  642.       colours = 32;
  643.  
  644.    if (NOT OpenColorWindow(screen, colours))
  645.       return;
  646.    FOREVER {
  647.       Wait((1L << ColorWindow->UserPort->mp_SigBit));
  648.       while (message = (struct IntuiMessage *)GetMsg(ColorWindow->UserPort)) {
  649.      class = message->Class;
  650.      code = message->Code;
  651.      x = message->MouseX;
  652.      y = message->MouseY;
  653.      if (class != MOUSEBUTTONS)
  654.         gadget = (struct Gadget *)(message->IAddress);
  655.      ReplyMsg(message);
  656.      switch (class) {
  657.         case GADGETDOWN:
  658.         case GADGETUP:
  659.            if ( NOT GadgetGotten(gadget->GadgetID))
  660.           return;
  661.            break;
  662.         case MOUSEBUTTONS:
  663.            if ( x >= CURRENT_LEFT && x < CURRENT_LEFT + 8 * 15)
  664.           if (y >= COLORS_TOP && y < COLORS_TOP + 4 * 10)
  665.              {
  666.              number = (x-CURRENT_LEFT)/15 + ((y-COLORS_TOP)/10)*8;
  667.              if (number < colours && code == SELECTDOWN)
  668.             colorchosen(number);
  669.              }
  670.            break;
  671.         case MOUSEMOVE:
  672.            ModifyColors();
  673.            break;
  674.         default:
  675.            break;
  676.      }
  677.       }
  678.    }
  679. }
  680.  
  681.  
  682.  
  683. DrawBox(rp, x1, y1, x2, y2)
  684. struct RastPort *rp;
  685. short     x1, y1, x2, y2;
  686. /* draws a box without overlapping the edges (in case of complement mode) */
  687. {
  688.    Move(rp, x1, y1 + SIGN(y2 - y1));
  689.    Draw(rp, x1, y2);
  690.    Move(rp, x1 + SIGN(x2 - x1), y2);
  691.    Draw(rp, x2, y2);
  692.    Move(rp, x2, y2 - SIGN(y2 - y1));
  693.    Draw(rp, x2, y1);
  694.    Move(rp, x2 - SIGN(x2 - x1), y1);
  695.    Draw(rp, x1, y1);
  696. }
  697.  
  698.  
  699.  
  700. complementbox(rp, colours)
  701. short colours;
  702. {
  703. short x, y;
  704.  
  705.    x = CURRENT_LEFT + (colours % 8) * 15;
  706.    y = COLORS_TOP + (colours / 8) * 10;
  707.  
  708.    SetDrMd(rp, COMPLEMENT);
  709.    DrawBox(rp, x, y, x + 14, y + 9);
  710.    SetDrMd(rp, JAM1);
  711. }
  712.  
  713.  
  714.  
  715. printtext(text, x, y)
  716. char     *text;
  717. short     x, y;
  718. {
  719.    string.IText = text;
  720.    PrintIText(rstport, &string, x, y);
  721. }
  722.